//
//  MXHomeInviteController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/5.
//

#import "MXHomeInviteController.h"
#import "MxPublicNav.h"
#import "MXShiftInController.h"
#import "zhPopupController.h"
#import "MXAcceptSuccessV.h"
@interface MXHomeInviteController () <MxNavigationViewDelegate>
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *countLab;
@property (nonatomic, strong) UILabel *nodataLab;
@property (nonatomic, strong) UILabel *accptLab;
@property (nonatomic, strong) UILabel *refuseLab;
@property (nonatomic, strong) UILabel *enterLab;
@property (nonatomic, strong) MXNoticeListModel *detailModel;
@property (nonatomic, strong) UILabel *noticeLab;
@end

@implementation MXHomeInviteController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.detailModel = [[MXNoticeListModel alloc] init];
    [self createUI];
    [self getNoticeDetail];
}

- (void)getNoticeDetail {

    NSString *urlStr = [NSString stringWithFormat:@"%@?noticeDetailId=%@",@"api/app/notice/view",SAFEPARAMETER(self.model.ID)];
    MJWeakSelf
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:urlStr params:@{} httpMethod:DCHttpRequestGet sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"redDismiss" object:self.model.appNotice.ID];
            weakSelf.detailModel = [MXNoticeListModel modelWithDictionary:dict[@"data"]];
            weakSelf.nameLab.text = weakSelf.detailModel.appNotice.createBy;
            weakSelf.noticeLab.text = weakSelf.detailModel.appNotice.noticeContent;
            if (weakSelf.detailModel.handleStatus == 2 || weakSelf.detailModel.handleStatus ==3 ) {
                self.refuseLab.hidden = self.accptLab.hidden = YES;
                self.enterLab.hidden = NO;
            }else{
                self.refuseLab.hidden = self.accptLab.hidden = NO;
                self.enterLab.hidden = YES;
            }
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)createUI {
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"家庭邀请";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    self.nameLab = [[UILabel alloc] init];
    self.nameLab.textAlignment = NSTextAlignmentCenter;
    self.nameLab.backgroundColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.1];
    self.nameLab.textColor = [UIColor colorWithHexString:@"#000000"];
    self.nameLab.font = PFRFontMedium(18);
//    self.nameLab.text = @"用户437779";
    self.nameLab.layer.cornerRadius = 3;
    self.nameLab.layer.masksToBounds = YES;
    [self.view addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.right.offset(-15);
        make.height.mas_equalTo(45);
        make.top.offset(kNavBarHeight +40);
    }];
    
    self.noticeLab = [[UILabel alloc] init];
//    self.noticeLab.text = @"邀请您加入“我的家”";
    self.noticeLab.textColor = [UIColor colorWithHexString:@"#000000"];
    self.noticeLab.font = PFRFont(14);
    [self.view addSubview:self.noticeLab];
    [self.noticeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(10);
    }];
    
    self.countLab = [[UILabel alloc] init];
    self.countLab.text = @"家庭设备（0个）";
    self.countLab.textColor = [UIColor colorWithHexString:@"#000000"];
    self.countLab.font = PFRFontMedium(14);
    [self.view addSubview: self.countLab];
    [ self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.top.mas_equalTo(self.noticeLab.mas_bottom).offset(43);
    }];
    
    self.nodataLab = [[UILabel alloc] init];
    self.nodataLab.text = @"暂无设备";
    self.nodataLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.6];
    self.nodataLab.font = PFRFont(13);
    [self.view addSubview: self.nodataLab];
    [ self.nodataLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.countLab.mas_bottom).offset(58);
    }];
    
    MJWeakSelf
    UILabel *refuseLab = [[UILabel alloc] init];
    refuseLab.userInteractionEnabled = YES;
    refuseLab.textAlignment = NSTextAlignmentCenter;
    refuseLab.text = @"拒绝";
    refuseLab.textColor = [UIColor colorWithHexString:@"#DF0505"];
    refuseLab.font = PFRFontMedium(16);
    refuseLab.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    refuseLab.layer.cornerRadius = 24;
    refuseLab.layer.masksToBounds = YES;
    [refuseLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf refuseEnter];
    }]];
    [self.view addSubview:refuseLab];
    [refuseLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.bottom.offset(-48);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    
    UILabel *acceptLab = [[UILabel alloc] init];
    acceptLab.userInteractionEnabled = YES;
    acceptLab.textAlignment = NSTextAlignmentCenter;
    acceptLab.text = @"接受邀请";
    acceptLab.textColor = [UIColor colorWithHexString:@"#FEFEFE"];
    acceptLab.font = PFRFontMedium(16);
    acceptLab.backgroundColor = [UIColor colorWithHexString:@"#3278F6"];
    acceptLab.layer.cornerRadius = 24;
    acceptLab.layer.masksToBounds = YES;
    [acceptLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf sureEnter];
    }]];
    [self.view addSubview:acceptLab];
    [acceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.bottom.mas_equalTo(refuseLab.mas_top).offset(-17);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    
    self.enterLab = [[UILabel alloc] init];
    self.enterLab.userInteractionEnabled = YES;
    self.enterLab.textAlignment = NSTextAlignmentCenter;
    self.enterLab.text = @"进入新家庭";
    self.enterLab.textColor = [UIColor colorWithHexString:@"#FEFEFE"];
    self.enterLab.font = PFRFontMedium(16);
    self.enterLab.backgroundColor = [UIColor colorWithHexString:@"#3278F6"];
    self.enterLab.layer.cornerRadius = 24;
    self.enterLab.layer.masksToBounds = YES;
    [self.enterLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf enterNewHome];
    }]];
    [self.view addSubview:self.enterLab];
    [self.enterLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.bottom.offset(-48);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    self.enterLab.hidden = YES;
    
    self.refuseLab = refuseLab;
    self.accptLab = acceptLab;
    self.refuseLab.hidden = YES;
    self.accptLab.hidden = YES;
}

- (void)refuseEnter {
    
//    [self.navigationController popViewControllerAnimated:YES];
    MJWeakSelf
    NSDictionary *param = @{@"handleStatus":@"3",@"noticeDetailId":SAFEPARAMETER(self.model.appNotice.ID)};
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/notice/handle" params:param httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"handleinvitesuccess" object:nil];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"handleinvitesuccess" object:nil];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } else {
            
            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)sureEnter {

    MJWeakSelf
    NSDictionary *param = @{@"handleStatus":@"2",@"noticeDetailId":SAFEPARAMETER(self.model.appNotice.ID)};
    [self.view makeToastActivity];
    [[DCHttpClient shareClient] requestWithPath:@"api/app/notice/handle" params:param httpMethod:DCHttpRequestPost sucess:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSDictionary * dict = [responseObject mj_JSONObject];
        [weakSelf.view hideToastActivity];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"handleinvitesuccess" object:nil];
        if (dict[DC_ResultCode_Key] && [dict[DC_ResultCode_Key] integerValue] == DC_Result_Success) {
            [weakSelf showAleart];
        } else {

            [weakSelf.view xjmakeToast:dict[DC_ResultMsg_Key]];
        }
    } failture:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        [weakSelf.view hideToastActivity];
    }];
}

- (void)showAleart {
    
    MXAcceptSuccessV *aleartV = [[MXAcceptSuccessV alloc] init];
    zhPopupController *zh_popupController = [[zhPopupController alloc] initWithView:aleartV size:aleartV.bounds.size];
    zh_popupController.presentationStyle = zhPopupSlideStyleFromTop;
    zh_popupController.layoutType = zhPopupLayoutTypeTop;
    zh_popupController.dismissOnMaskTouched = YES;
    zh_popupController.maskAlpha = 0;
    zh_popupController.offsetSpacing = kStatusBarHeight;
    [zh_popupController showInView:self.view duration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut bounced:NO completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [zh_popupController dismiss];
    });
    self.enterLab.hidden = NO;
    self.accptLab.hidden = self.refuseLab.hidden = YES;
}

- (void)enterNewHome {
//    MXShiftInController *inVc = [[MXShiftInController alloc] init];
//    [self.navigationController pushViewController:inVc animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"enterNewFamily" object:self.model.familyId];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

-(void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}


@end
